package com.luyuan.crm.sale.web;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.luyuan.crm.sale.model.Order;
import com.luyuan.crm.sale.model.OrderProduct;
import com.luyuan.crm.sale.service.OrderService;
import com.ruijie.web.jsf.action.BaseAction;

@Component("orderPrintAction")
@Scope("view")
public class OrderPrintAction extends BaseAction implements InitializingBean {

    @Autowired
    private OrderService orderService;

    private Order order;

    private List<OrderProduct> orderProducts = null;

    private String orderId;

    public Order getOrder() {
        if (order == null) {
            order = orderService.getById(orderId);
            orderService.increasePrintCount(order);
            orderProducts = new ArrayList<OrderProduct>(order.getOrderProducts());

            Collections.sort(orderProducts);
        }
        if (order == null) {
            order = new Order();
        }
        return order;
    }

    public List<OrderProduct> getOrderProducts() {
        return orderProducts;
    }

    public void setOrder(Order order) {
        this.order = order;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        orderId = getRequestParameter("orderId");
    }

}
